<?php

class Default_SearchController extends ExtLb_Controller_Action
{
	private $postTbl;
	private $commentTbl;

    public function init(){
        $this->loadCss('default', 'search', FOLDER_ROOT_NAME);

        $this->postTbl	= new Admin_Model_Posts();
        $this->commentTbl = new Admin_Model_Comments();
        
        //RANDOM POST (BAI VIET NOI BAT)
    	$randPost = $this->postTbl->selectPost('bv.trangThai =1','baiVietId DESC',10)->query()->fetchAll();
    	$this->view->randPosts = $randPost;
    	
    	$this->view->comments = $this->commentTbl->selectComment(NULL,'yKienId DESC',5)->query()->fetchAll();
    }
    
    public function indexAction(){
        if (isset($_GET['search_box'])){
        	if(!empty($_GET['search_box'])){
        		$params = array(
        					'baiVietId'	=> 40,
        					'tieuDe'	=> 'test',
        					 'noiDung'	=>	'test thu'
        				);
        		$result = $this->postTbl->searchItem($params);
        		foreach ($result as $value) {
        			echo 'ID: '.$value->new_id ." 
        				tieu de: ". $value->tieude ."  
        				noi dung: ".$value->noiDung ."<br>";
        			
        		}
        		
//        		$selectPost = $this->postTbl->selectPost("bv.trangThai =1 and bv.tieuDe like '%".$_GET['search_box']."%'");
//	        	$paginatorPosts = new Zend_Paginator(new Zend_Paginator_Adapter_DbSelect($selectPost));
//	        	$paginatorPosts->setItemCountPerPage(20)
//	        					->setPageRange(3)
//	        					->setCurrentPageNumber($this->_getParam('page',1));
//	        		
//	        	$this->view->paginatorPosts = $paginatorPosts;	
        	}else{
        		$this->jShowMessage("Vui lòng nhập từ khóa tìm kiếm trước khi tìm!!!");	
        	}
        }else {
        	$this->_redirect('/default/');
        }
    }
    
    public function addAction(){
    	$params = array(    				
    				'tieuDe'	=> 	'This is test',
    				'noiDung'	=>	'The example for this is test'
    			);
    	$id = $this->postTbl->addCustomPost($params);
    	echo $id ."dddddddddd"; 
    	$params['baiVietId'] = $id;
    	$this->postTbl->saveAddItem($params, array('task'=>'add'));
    	
    }
}

